﻿using System.Collections.Generic;
using System.Linq;
using UcAsp.Net.Model;

namespace UcAsp.Net.MSSQLDAL
{
    public class AdminUserDAL : UcAsp.Net.IDAL.IAdminUserDAL
    {
        public AdminUser Login(string username, string password)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                UcAsp.Net.Model.AdminUser userinfo = entities.AdminUsers.FirstOrDefault(o => o.UserName == username && password == o.UserPassword);
                return userinfo;
            }
        }

        public bool Insert(AdminUser user)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                entities.AdminUsers.Add(user);
                entities.SaveChanges();
                return true;
            }
        }

        public bool Update(AdminUser user)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                System.Data.Entity.Infrastructure.DbEntityEntry<AdminUser> entry = entities.Entry<AdminUser>(user);
                entry.State = System.Data.EntityState.Modified;
                entities.SaveChanges();
                return true;
            }
        }

        public bool Delete(AdminUser user)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                AdminUser userinfo = entities.AdminUsers.FirstOrDefault(o => o.ID == user.ID);
                entities.AdminUsers.Remove(userinfo);
                entities.SaveChanges();
                return true;
            }
        }

        public List<AdminUser> Select()
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                List<AdminUser> list = entities.AdminUsers.ToList();
                return list;
            }
        }

        public AdminUser GetAdminByUserName(string username)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                UcAsp.Net.Model.AdminUser userinfo = entities.AdminUsers.FirstOrDefault(o => o.UserName == username);
                return userinfo;
            }
        }
    }
}